﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IMark.DAO
{
    public class BaiVietModels
    {
        public static List<BAI_VIET> GetAll()
        {
            using (var context = new EMartEntities())
            {
                return context.BAI_VIET.Include("NGUOI_DUNG").Include("LOAI_BAI_VIET").Where(w=>w.TrangThai==1).OrderByDescending(u => u.ID).ToList();
            }
        }
        public static BAI_VIET GetByID(int ID)
        {
            using (var context = new EMartEntities())
            {
                return context.BAI_VIET.Find(ID);
            }
        }
        public static bool Add(BAI_VIET baiViet)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    context.BAI_VIET.Add(baiViet);
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static bool Update(BAI_VIET baiViet)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    BAI_VIET existing = context.BAI_VIET.Find(baiViet.ID);
                    existing.Hinh = baiViet.Hinh;
                    existing.IDLoaiBaiViet = baiViet.IDLoaiBaiViet;
                    existing.NoiDungChiTiet = baiViet.NoiDungChiTiet;
                    existing.NoiDungTomTat = baiViet.NoiDungTomTat;
                    existing.TieuDe = baiViet.TieuDe;                                        
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static bool UpdateStatus(int ID,byte Status)
        {            
            using (var context = new EMartEntities())
            {
                try
                {
                    BAI_VIET existing = context.BAI_VIET.Find(ID);
                    if(Status == 1)
                        existing.TrangThai = 0;                    
                    else
                        existing.TrangThai = 1;                    
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static bool Delete(long ID)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    BAI_VIET baiViet = context.BAI_VIET.Find(ID);
                    context.BAI_VIET.Remove(baiViet);
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
    }
}